﻿using MessagePack;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class TestRead : MonoBehaviour
{
    private void Start()
    {
        //SaveItemData();
        LoadItemData();
    }
    public void SaveItemData()
    {
        var AllItemObj = FindObjectsOfType(typeof(GameObject)) as GameObject[]; //找到场景中所有的GameObject类型的对象
        ItemData itemData = new ItemData(); //实例化一个ItemData用来存值
        foreach (GameObject child in AllItemObj)
        {
           // if (child.gameObject.layer == 1)  //10: Ornament   找到层级为10的GameObject
            {
                //Debug.Log(child.gameObject.name); //这里可以打印出名字试一试
                itemData.Name.Add(child.gameObject.name);  //这下面是保存每一个物体的Transform信息
                itemData.Layer.Add(child.gameObject.layer);
                itemData.position_x.Add(child.gameObject.transform.position.x);
                itemData.position_y.Add(child.gameObject.transform.position.y);
                itemData.position_z.Add(child.gameObject.transform.position.z);
                itemData.rotation_x.Add(child.gameObject.transform.rotation.eulerAngles.x);
                itemData.rotation_y.Add(child.gameObject.transform.rotation.eulerAngles.y);
                itemData.rotation_z.Add(child.gameObject.transform.rotation.eulerAngles.z);
                itemData.scale_x.Add(child.gameObject.transform.localScale.x);
                itemData.scale_y.Add(child.gameObject.transform.localScale.y);
                itemData.scale_z.Add(child.gameObject.transform.localScale.z);
            }
        }
        //注意:这里用到了MessagePack的序列化方法把ItemData类型的itemData对象序列化为一个byte数组
        var byteData = MessagePackSerializer.Serialize<ItemData>(itemData);
        //必须要在Unity中工程中创建一个StreamingAssets的文件夹
        string path = Application.streamingAssetsPath + "/";
        //保存数据的文件的名字
        string name = "ItemData";
        //把数据全部写入文件中
        File.WriteAllBytes(path + name, byteData);
        Debug.Log("保存完毕");
    }
    public void LoadItemData()
    {
        UnityEngine.Object itemObj;
        GameObject IObj = null;
        string path = Application.streamingAssetsPath + "/";
        string name = "ItemData";
        string itemPath = "Models/Prefabs/";
        byte[] readByte;
        if (File.Exists(path + name))
        {
            readByte = File.ReadAllBytes(path + name);
            Debug.Log("读取数据完毕");
        }
        else
        {
            Debug.Log("没找到路径，当前得到的路径是: " + path + name);
            return;
        }
        ItemData itemData = new ItemData();
        //注意:这里用到了MessagePack的反序列化方法，这里是把readByte以ItemData的类型反序列化出来，赋值给itemData
        itemData = MessagePackSerializer.Deserialize<ItemData>(readByte);
        for (int i = 0; i < itemData.Name.Count; i++) //把序列化出来的数据传给每一个预制体
        {
            string nameNoNum = itemData.Name[i];
            itemObj = Resources.Load(itemPath + nameNoNum, typeof(GameObject));
            IObj = Instantiate(itemObj) as GameObject; //实例化预制体
            IObj.name = itemData.Name[i];
            IObj.layer = itemData.Layer[i];

            IObj.transform.position = new Vector3(itemData.position_x[i], itemData.position_y[i], itemData.position_z[i]);
            IObj.transform.rotation = Quaternion.Euler(new Vector3(itemData.rotation_x[i], itemData.rotation_y[i], itemData.rotation_z[i]));
            IObj.transform.localScale = new Vector3(itemData.scale_x[i], itemData.scale_y[i], itemData.scale_z[i]);
        }
        Debug.Log("加载完毕");
    }
}
